﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Data.Entities;
using System.Web.Mvc.Html;
using Proggy.Web.Cms.ContentModel;

namespace Proggy.Web.Cms.Extensions.Html
{
    /// <summary>
    /// Html url extensions
    /// </summary>
    public static class ProggyLinkExtensions
    {
        #region overloads

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, null, null, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, null, null, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model, string action)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, null, action, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model, string action)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, null, action, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model, string action, int id)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, null, action, id);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model, string action, int id)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, null, action, id);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model, object htmlAttributes)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, htmlAttributes, null, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model, object htmlAttributes)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, htmlAttributes, null, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model, object htmlAttributes, string action)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, htmlAttributes, action, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model, object htmlAttributes, string action)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, htmlAttributes, action, null);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, IContentModel model, object htmlAttributes, string action, int id)
        {
            return ProggyLinkExtensions.proggyLink(html, model, null, htmlAttributes, action, id);
        }

        /// <summary>
        /// Proggy model action link
        /// </summary>
        public static MvcHtmlString ContentActionLink(this HtmlHelper html, string linkText, IContentModel model, object htmlAttributes, string action, int id)
        {
            return ProggyLinkExtensions.proggyLink(html, model, linkText, htmlAttributes, action, id);
        }

        #endregion

        #region private over-arching method

        /// <summary>
        /// Private method allowing different ways to create action link
        /// </summary>
        /// <param name="html"></param>
        /// <param name="model"></param>
        /// <param name="linkText"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="actionName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private static MvcHtmlString proggyLink(HtmlHelper html, IContentModel model, string linkText, object htmlAttributes, string actionName, int? id)
        {
            // check link text
            if (string.IsNullOrEmpty(linkText))
            {
                linkText = model.Name;
            }

            // check action name
            if (string.IsNullOrEmpty(actionName))
            {
                actionName = "HandleUrl";//string.Empty;
            }

            // sort routing obj
            object routingObj = null;

            // check id
            if (id == null)
            {
                routingObj = new
                {
                    controller = model.TypeAlias,
                    //url = model.UrlPortion,
                    nodeId = model.Id
                };
            }
            else
            {
                routingObj = new
                {
                    controller = model.TypeAlias,
                    //url = model.UrlPortion,
                    nodeId = model.Id,
                    id = id
                };
            }


            // check for html attr
            if (htmlAttributes != null)
            {
                return LinkExtensions.ActionLink(html, linkText, actionName, routingObj, htmlAttributes);
            }
            else
            {
                return LinkExtensions.ActionLink(html, linkText, actionName, routingObj);
            }
        }

        #endregion
    }
}
